package cn.wizzer.app.task.commons.ext.quartz.job;

import cn.wizzer.app.crown.modules.services.order.CrownOrderService;
import cn.wizzer.app.sys.modules.services.SysTaskService;
import cn.wizzer.app.user.modules.services.account.AccountRecordStatisticsService;
import cn.wizzer.framework.util.DateUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.Times;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;

import java.util.TimeZone;

/**
 * @author zl
 * @date 2020/6/17 9:56
 */
@IocBean
public class UserAccountRecordStatisticsJob implements Job {
    private static final Log log = Logs.get();

    @Inject
    @Reference(retries = 3, timeout = 500000)
    private AccountRecordStatisticsService accountRecordStatisticsService;

    @Inject
    @Reference(retries = 3, timeout = 500000)
    private CrownOrderService orderService;


    @Inject
    @Reference
    private SysTaskService sysTaskService;

    /**
     * context中startDate、endDate值为空字符串则只统计前一天。
     * startDate值为"2021-02-01",endDate值为"2021-03-10"时,统计2021-02-01到2021-03-09每天的流水。
     * startDate值为"2021-02-01",endDate值为空字符串时,统计2021-02-01到系统时间前一天的流水
     *
     * @param context
     */
    @Override
    public void execute(JobExecutionContext context) {
        log.info("统计用户流水记录开始");
        JobDataMap data = context.getJobDetail().getJobDataMap();
        // 获取参数时间区域
        String startDate = data.getString("startDate");
        String endDate = data.getString("endDate");
        this.statistics(startDate, endDate);
        String taskId = context.getJobDetail().getKey().getName();
        sysTaskService.update(Chain.make("exeAt", (int) (System.currentTimeMillis() / 1000))
                .add("exeResult", "执行成功"), Cnd.where("id", "=", taskId));
        log.info("统计用户流水记录结束");
    }


    /**
     * 统计每日流水数据,不包括‘体验金投资人数’的统计
     *
     * @param startDate
     * @param endDate
     */
    private void statistics(String startDate, String endDate) {
        // 如果参数中有时间区域则按区域统计
        if (Strings.isNotBlank(startDate)) {
            // 计算系统时间当天距离endDate的天数
            int days = 0;
            // 如果endTime为空，则endTime为系统时间当天
            if (Strings.isBlank(endDate)) {
                endDate = Times.format("yyyy-MM-dd", Times.D(System.currentTimeMillis()));
            } else {
                days = (int) ((System.currentTimeMillis() - (Times.ams(endDate))) / (24 * 60 * 60 * 1000));
            }
            // 计算时间区域之间的天数
            int dayNum = (int) ((Times.ams(endDate) - Times.ams(startDate)) / (24 * 60 * 60 * 1000));
            // 倒序循环统计每天流水。3月9、3月8.....startDate
            for (int i = 1; i <= dayNum; i++) {
                long yesZeroLong = DateUtil.getZeroLong(-i - days, TimeZone.getDefault().getID());
                long zeroLong = DateUtil.getZeroLong(-i - days + 1, TimeZone.getDefault().getID());
                accountRecordStatisticsService.statistics(yesZeroLong * 1000, zeroLong * 1000);
                orderService.expInvestStatistics(yesZeroLong, zeroLong);
            }
        }
        // 否则默认统计前一天
        else {
            long yesZeroLong = DateUtil.getZeroLong(-1, TimeZone.getDefault().getID());
            long zeroLong = DateUtil.getZeroLong(0, TimeZone.getDefault().getID());
            accountRecordStatisticsService.statistics(yesZeroLong * 1000, zeroLong * 1000);
            orderService.expInvestStatistics(yesZeroLong, zeroLong);
        }
    }

}
